<?php

namespace app\api\controller;

use addons\shopro\service\user\User as UserService;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\Children;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\goods\SkuPrice;
use app\admin\model\shopro\order\Action;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\order\OrderItem;
use app\admin\model\shopro\user\Silverlog;
use app\admin\model\shopro\user\User as UserModel;
use app\admin\model\shopro\user\WalletLog;
use app\admin\model\user\Prizelog;
use app\common\controller\Api;
use app\common\model\GoldenLog;
use app\common\model\GoldLog;
use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use app\common\model\shopro\DividendLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use app\common\model\shopro\ReleaseAccelerateMarketplaceLogModel;
use app\common\model\shopro\ReleaseLog;
use app\common\model\shopro\Shop;
use app\common\model\shopro\UserNormalPrizeLogModel;
use app\common\model\shopro\UserTeamDayWalletLogModel;
use app\common\model\User;
use app\common\model\Userchildren;
use app\common\model\UserStaticLogModel;
use fast\Random;
use think\Db;
use think\exception\HttpResponseException;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function ceshi(){
        $prizeLogModel = new Prizelog();
        $data          = $prizeLogModel
            ->where('day_time','<',time())->where('status', '<', 2)->select();
        var_dump($data);
    }

    /**
     * @menu:普通会员奖励
     * @Author:男人的坏
     * @Date:2024-07-20 11:49
     */

    public function user_normal($user, $prize_money, $prizeAppointInfo, $userModel)
    {
//        $time             = date('Ymd');
//        $user_create_time = date('Ymd', $user['createtime']);
        $user_ids_arr = Userchildren::where(['children_id' => $user['id']])->column('member_id');
        $userData     = $userModel->whereIn('id', $user_ids_arr)->where('is_qrcode', '=',
            1)->field('id,total_consume,level')->order('id desc')->select();
        if ($userData) {
            foreach ($userData as $k => $v) {
                $count = $userModel->where(['parent_user_id' => $v['id'], 'is_qrcode' => 1])->count();
                if ($count >= 3) {
                    $money = bcmul($prize_money, config('site.game_dividend') / 100);
                    dump($money);
                    dump($v['id']);
//                    if ($money) {
//                        WalletService::change($v['id'], 'score', $money, 'game_dividend', [],
//                            '团队游戏流水返利');
//                    }
//                    if ($prizeAppointInfo['type'] == 2) {
//                        $inser['user_id']      = $v['id'];
//                        $inser['draw_user_id'] = $user['id'];
//                        $inser['prize_id']     = $prizeAppointInfo['id'];
//                        UserNormalPrizeLogModel::create($inser);
//                    }
                    if($v['level'] > 0){
                        break;
                    }
                }
            }
        }
    }

    /**
     * @menu:记录团队业绩
     * @Author:男人的坏
     * @Date:2024-07-20 09:46
     */
    public function team_yeji($user, $prize_money, $userModel)
    {
        $user_ids_arr = Userchildren::where(['children_id' => $user['id']])->column('member_id');
        $userData     = $userModel->whereIn('id', $user_ids_arr)->where('is_qrcode', '=',
            1)->field('id,total_consume')->select();
        if ($userData) {
            $dayLogModel = new UserTeamDayWalletLogModel();
//            foreach ($userData as $k => $v) {
//                $dayLogInfo = $dayLogModel->whereTime('createtime', 'd')->where(['user_id' => $v['id']])->find();
//                if ($dayLogInfo) {
//                    $dayLogInfo->amount += $prize_money;
//                    $dayLogInfo->save();
//                } else {
//                    $inser['user_id'] = $v['id'];
//                    $inser['endtime'] = strtotime(date('Y-m-d 00:00:01', strtotime("+1 day")));
//                    $inser['amount']  = $prize_money;
//                    $dayLogModel->create($inser);
//                }
//                $v->setInc('total_consume', $prize_money);
//            }
            $this->Upgrades($userData, $userModel);
        }
    }

    /**
     * @menu 升级
     * @Author:男人的坏
     * @Date:2024-07-20 09:22
     */
    public function Upgrades($userData, $userModel)
    {
        foreach ($userData as $key => $user) {
            $parent_user_data = $userModel->where([
                'parent_user_id' => $user['id'],
                'is_qrcode'      => 1
            ])->field('id,total_consume + user_water_log as total')->order('total desc')->select();
            if (count($parent_user_data) >= 3) {
                $total_consume = 0;
                foreach ($parent_user_data as $k => $v) {
                    if ($k > 0) {
                        $total_consume += $v['total'];
                    }
                }
                if ($total_consume) {
                    $parent_user = $userModel->where(['id' => $user['id']])->find();
                    $this->consume($parent_user, $total_consume);
                }
            }
        }
    }

    public  function consume($user, $amount)
    {
        // 判断金额
        if ($amount == 0) {
            return false;
        }

        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user['id']);
        }
        if (!$user) {
            error_stop('未找到用户');
        }
        $accelerationModel    = new Acceleration();
        $where['id']          = ['neq', $user['level']];
        $where['performance'] = ['elt', $amount];
        $where['status']      = 1;
        $accelerationInfo     = $accelerationModel->where($where)->order('id desc')->find();
//        dump($accelerationInfo);
//        dump($user['id']);
        if ($accelerationInfo) {
//            dump($user['id']);
//            dump($accelerationInfo->id);
            if($user['level'] < $accelerationInfo->id){
                dump($accelerationInfo->id);
                dump($user['id']);
//                db('user')->where(['id'=>$user['id']])->update(['level'=> $accelerationInfo->id]);
            }
        }
        // 更新会员代金券信息
//        $user->setInc('total_consume', $amount);
        //判断是否能升级
        return true;
    }

    public function digui(){

    }
}
